package data;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import fileProcessing.ParsingException;

public class SSNEntry extends AbstractDataType {
	String fieldType = "SSN";
	int ssn;

	public SSNEntry(RandomAccessFile raf) throws ParsingException{
		try {
			ssn = raf.readInt();
		} catch (IOException e) {
			throw new ParsingException("Error: "+e.getMessage()+" when reading ssn from table's data file");
		}
	}
	
	public SSNEntry(String ssnVal) throws ParsingException{
		if(isValid(ssnVal)){
			ssn = Integer.parseInt(ssnVal);
		}
		else
			throw new ParsingException("ssn not valid.");
	}

	public SSNEntry() {}

	@Override
	public int compareTo(Object arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public boolean isValid(String val) throws ParsingException {
		Pattern pattern = Pattern.compile("\\d{1,17}");
		Matcher matcher = pattern.matcher(val);
		return matcher.matches();
	}

	@Override
	public String getType() {
		return fieldType;
	}

}
